//
//  SendPhotoView.m
//  自定义聊天界面和键盘-多选照片-语音-表情-文本
//
//  Created by qinlongqing on 14-10-14.
//  Copyright (c) 2014年 qinlongqing. All rights reserved.
//

#import "SendPhotoView.h"
#import "QinKeyBoardController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@implementation SendPhotoView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    UIButton *recordingButton = [[UIButton alloc]initWithFrame:CGRectMake((self.frame.size.width - 196)/3, (kSendPhotoViewFrameHeight - 98)/2-15, 98, 98)];
    [recordingButton setImage:[UIImage imageNamed:@"聊天_未选中图片.png"] forState:UIControlStateNormal];
    [recordingButton setImage:[UIImage imageNamed:@"聊天_选中图片.png"] forState:UIControlStateHighlighted];

    [recordingButton addTarget:self action:@selector(doSendPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:recordingButton];
    UIImageView *photoImage=[[UIImageView alloc] initWithFrame:CGRectMake(recordingButton.center.x-2.5, (kSendPhotoViewFrameHeight - 98)/2+90, 5, 5)];
    photoImage.layer.cornerRadius=3;
    photoImage.backgroundColor=[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16];
    [self addSubview:photoImage];
    
    UILabel *photoLabel=[[UILabel alloc]initWithFrame:CGRectMake((self.frame.size.width - 196)/3, (kSendPhotoViewFrameHeight - 98)/2+100, 98, 20)];
    photoLabel.text=@"相册";
    photoLabel.textAlignment=NSTextAlignmentCenter;
    photoLabel.font=[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
    photoLabel.textColor=[UIColor blackColor];
    [self addSubview:photoLabel];
    
    
    
    
    UIButton *carmeraButton = [[UIButton alloc]initWithFrame:CGRectMake((self.frame.size.width - 196)*2/3+98, (kSendPhotoViewFrameHeight - 98)/2-15, 98, 98)];
    [carmeraButton setImage:[UIImage imageNamed:@"聊天_未选中相机.png"] forState:UIControlStateNormal];
    [carmeraButton setImage:[UIImage imageNamed:@"聊天_选中相机.png"] forState:UIControlStateHighlighted];
    [carmeraButton addTarget:self action:@selector(doSendCarmera:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:carmeraButton];
    
    UIImageView *carmeraImage=[[UIImageView alloc] initWithFrame:CGRectMake(carmeraButton.center.x-2.5, (kSendPhotoViewFrameHeight - 98)/2+90, 5, 5)];
    carmeraImage.layer.cornerRadius=3;
    carmeraImage.backgroundColor=[ClassMethodsViewController colorWithHexString:NACIGATIONBAR_BACKGROUNDCOLOR_16];
    [self addSubview:carmeraImage];
    
    UILabel *carmeraLabel=[[UILabel alloc]initWithFrame:CGRectMake((self.frame.size.width - 196)*2/3+98, (kSendPhotoViewFrameHeight - 98)/2+100, 98, 20)];
    carmeraLabel.text=@"拍照";
    carmeraLabel.textAlignment=NSTextAlignmentCenter;
    carmeraLabel.font=[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
    carmeraLabel.textColor=[UIColor blackColor];
    [self addSubview:carmeraLabel];

}

- (void)doSendPhoto:(UIButton *)sender{
//    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    
    ALAuthorizationStatus status=[ALAssetsLibrary authorizationStatus];
    if (status!=ALAuthorizationStatusAuthorized) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"照片被禁用"
                                                       message:@"请在iPhone的“设置-隐私-照片”中允许安好访问您的照片。"
                                                      delegate:nil
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil, nil];
        [alert show];
    }
    
    
    
    else
    {
        //允许了
        if (self.photoDelegate&&[self.photoDelegate respondsToSelector:@selector(selectPhotos)]) {
            [self.photoDelegate selectPhotos];
        }
    }
}
- (void)doSendCarmera:(UIButton *)sender{
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus != AVAuthorizationStatusAuthorized)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"相机被禁用"
                                                       message:@"请在iPhone的“设置-隐私-相机”中允许安好访问您的相机。"
                                                      delegate:nil
                                             cancelButtonTitle:@"确定"
                                             otherButtonTitles:nil, nil];
        [alert show];
    }else
    {
        //允许了
//        if ([[VideoManagerCenter instance] isOnlineChat]) {
//            [self showCustomHudSingleLine:@"视频通话中无法拍摄照片"];
//            return;
//        }
        if (self.photoDelegate&&[self.photoDelegate respondsToSelector:@selector(selectCarmera)]) {
            [self.photoDelegate selectCarmera];
        }
    }
    
}
-(void)showCustomHudSingleLine:(NSString *)text
{
    UIView *customHud = [[UIView alloc] init];
    customHud.tag = 888;
    
    int width = text.length * 16 + 40;
    int height = 60;
    customHud.frame = CGRectMake((kScreenWidth - width)/2, (kScreenHeight - height)/2, width, height);
    customHud.layer.masksToBounds = YES;
    customHud.layer.cornerRadius = 8;
    customHud.backgroundColor = [UIColor blackColor];
    customHud.alpha = 0;
    
    UILabel *contenLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, width - 40, height - 40)];
    contenLabel.text = text;
    contenLabel.textColor = [UIColor whiteColor];
    contenLabel.font = [UIFont boldSystemFontOfSize:16];
    contenLabel.numberOfLines = 0;
    contenLabel.textAlignment = NSTextAlignmentCenter;
    [customHud addSubview:contenLabel];
    
    [[[UIApplication sharedApplication].delegate window] addSubview:customHud];
    
    [UIView animateWithDuration:0.3f animations:^{
        customHud.alpha = 0.8;
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1.5f animations:^{
            customHud.alpha = 0;
        } completion:^(BOOL finished) {
            customHud.hidden = YES;
            [customHud removeFromSuperview];
        }];
        
    }];
    
    //    [self performSelector:@selector(hideCustomHud) withObject:nil afterDelay:1.0f];
}
@end
